package com.wdress.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.hibernate.SQLQuery;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.transform.Transformers;

import com.wdress.util.HibernateUtil;

public abstract class BaseAction extends Action{
	
	public ActionForward execute(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
		Session session = null;
		Transaction tx = null;
		DynaActionForm form = (DynaActionForm)arg1;
		String lang = "en";
		try {
			session = HibernateUtil.getSessionFactory(arg2).openSession();
			tx = session.beginTransaction();
			lang=arg2.getSession().getAttribute("lang").toString();
			initLangUrl(arg2);
			executeAction(form, session, arg2, arg3,lang);
			initData(session, arg2, lang);
		}catch(Exception e){
			String urls=arg2.getRequestURI();
			String hostName = arg2.getServerName();
			System.out.println("ERROR:---"+hostName+urls+":"+e.getMessage());
			e.printStackTrace();
			return arg0.findForward("error");
		}
		finally {
			if(tx!=null){
				tx.commit();
			}
			if(session!=null ){
				session.close();
			}
		}
		return arg0.findForward("success");
	}
	
	public abstract String executeAction(DynaActionForm form,Session session, HttpServletRequest arg2,HttpServletResponse arg3,String lang) throws Exception;
	
	public void initData(Session session, HttpServletRequest arg2,String lang){
		SQLQuery sqlQuery = session.createSQLQuery("select keyword,url from wkeyword where types='categorie' and lang=?");
		sqlQuery.setString(0, lang);
		List categorielist = sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		arg2.setAttribute("categorielist", categorielist) ;
		sqlQuery = session.createSQLQuery("select keyword,url from wkeyword where types='keyword' and lang=?");
		sqlQuery.setString(0, lang);
		List keywordlist = sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setMaxResults(6).list();
		arg2.setAttribute("keywordlist", keywordlist) ;
		sqlQuery = session.createSQLQuery("select keyword,url from wkeyword where types='material' and lang=?");
		sqlQuery.setString(0, lang);
		List materiallist = sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setMaxResults(10).list();
		arg2.setAttribute("materiallist", materiallist) ;
	}
	
	public void initLangUrl(HttpServletRequest arg2){
		arg2.setAttribute("enUrl", "http://www.superbdress.com");
		arg2.setAttribute("frUrl", "http://fr.superbdress.com");
		arg2.setAttribute("esUrl", "http://es.superbdress.com");
		arg2.setAttribute("ruUrl", "http://ru.superbdress.com");
		arg2.setAttribute("ptUrl", "http://pt.superbdress.com");
	}
}
